package com.saifan.wyy_ov.ui.set;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;


import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.common.BaseActivity;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.ui.user.LoginActivity;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lomasky.ma.httpUtils.HttpUtil;
import lomasky.ma.httpUtils.ICallBack;
import lomasky.ma.httpUtils.JsonUtil;
import lomasky.ma.httpUtils.ResponseBean;
import lomasky.ma.utils.SharedPrefUtil;


/**
 * @author Eicky
 * @Description: 服务器设置页面
 * @date: 2016/7/1 15:14
 * @version: V1.0
 */
public class ServerSetActivity extends BaseActivity {


    private lomasky.ma.xui.Spinner spinner;
    private List<Company> companies = new ArrayList<>();
    private List<Project> projects = new ArrayList<>();
    private lomasky.ma.xui.Spinner spinnerxm;

    private TextView tip;



    @Override
    protected void onStart() {
        super.onStart();

            Project project =JsonUtil.getObject(SharedPrefUtil.getString(this,"SelectProject" ,""),Project.class);
            if (project != null) {
                tip.setText("你当前所在的项目为[ " + project.getXMName() + "] ,你可以通过下拉框切换到其它项目");
            } else {
                tip.setText("你当前还未选择项目，你可以通过下拉框选择一个可用的项目");
            }



    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server_set);
        this.tip = (TextView) findViewById(R.id.tip);




        this.spinnerxm = (lomasky.ma.xui.Spinner) findViewById(R.id.spinnerxm);
        this.spinner = (lomasky.ma.xui.Spinner) findViewById(R.id.spinner);
        spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                spinnerxm.setList(new ArrayList<String>());
                projects.clear();

                spinnerxm.setSelectedItemPosition(-1);
                spinnerxm.setText("");

                spinner.setText(spinner.getList().get(i));
                spinner.setSelectedItemPosition(i);
                spinner.popWindow.dismiss();


                List<String> stringList = new ArrayList<>();
                for (Project project : companies.get(i).getProject()) {
                    projects.add(project);
                    stringList.add(project.getXMName());
                }

                spinnerxm.setList(stringList);



            }
        });

        spinnerxm.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                spinnerxm.setText(spinnerxm.getList().get(i));
                spinnerxm.setSelectedItemPosition(i);
                spinnerxm.popWindow.dismiss();

                checkPro(projects.get(i));




            }


        });

    }

    private void checkPro( Project project) {
        boolean isTest = project.getP_SFQY() != 0;
        if (isTest) {
            String csTime = project.getP_JSSJ();
            if (TextUtils.isEmpty(csTime)) {
                showDialog("试用时间已过期！");
                return;
            }
            Date date = StringUtil.getYYYYMMDD(csTime);

            if (date != null) {
                if (date.before(new Date())) {
                    showDialog("试用时间已过期！");
                    return;

                }


            }else {
                showDialog("试用时间已过期！");
                return;
            }
        }



        Api.setUrlIp(project.getAPPURL());
        SharedPrefUtil.putString(ServerSetActivity.this,"SelectProject", JsonUtil.toJson(project));
        startActivity(LoginActivity.class);
        finish();

    }
    @Override
    protected void onResume() {
        super.onResume();
        loadCompanyAndProject();
    }

    private void loadCompanyAndProject() {
        Map<String, Object> map = new HashMap<>();
        map.put("action", "TBSJ");
        map.put("WYGSLX", 2);//（1思源2赛帆）
        map.put("XMLX", 2);
        HttpUtil.check(this,map, new ICallBack() {
            @Override
            public void err(String e) {

            }

            @Override
            public void ok(ResponseBean responseBean) {
                if (responseBean.ok) {
                    List<String> stringList = new ArrayList<>();
                    List<Company> list = JsonUtil.getList(responseBean.content, Company.class);

                    for (Company company : list) {
                        if (company.getProject()!=null||company.getProject().size()>0){
                            companies.add(company);
                            stringList.add(company.getCompanyName());
                        }
                    }

                    spinner.setList(stringList);
                    SharedPrefUtil. putString(ServerSetActivity.this,"Companys", JsonUtil.toJson(companies));


                } else {
                    ToastUtil.showToast(ServerSetActivity.this, "请求数据失败了");
                }

            }
        });
    }


    public void showDialog(String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });
        builder.setTitle("提示");
        builder.setMessage(msg);
        builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_BACK:
                        dialog.dismiss();

                        break;
                    default:
                        break;
                }
                return true;
            }
        });
        builder.setCancelable(false);
        builder.create().show();
    }


}
